﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using WooCoo.Serialization;
using WooCoo.DataPortal.Server.Hosts.WcfChannel;
using WooCoo.Data;

namespace WooCoo.DataPortal.Server.Hosts
{
    /// <summary>
    /// 数据门户的业务接入，Wcf方式
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WcfPortal : IWcfPortal
    {
        #region IWcfPortal Members

        /// <summary>
        /// Create a new business object.
        /// </summary>
        /// <param name="request">The request parameter object.</param>
        [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        public WcfResponse Create(CriteriaRequest request)
        {
            object criteria = request.CriteriaData;//GetCriteria(request.CriteriaData);
            Type bizType = Type.GetType(request.TypeName);
            DataPortalContext context = request.Context;//MobileFormatter.Deserialize(request.PortalContextData) as DataPortalContext;
            SimplePortal portal = new SimplePortal();
            var result = new WcfResponse();
            try
            {
                //result.ObjectData = portal.Create(bizType, criteria, context);
            }
            catch (Exception ex)
            {
                result.ErrorData = new ServiceExceptionDetail(ex);
            }
            return result;
        }

        /// <summary>
        /// Get an existing business object.
        /// </summary>
        /// <param name="request">The request parameter object.</param>
        [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        public WcfResponse Fetch(CriteriaRequest request)
        {
            object criteria = request.CriteriaData;//GetCriteria(request.CriteriaData);
            Type bizType = Type.GetType(request.TypeName);
            DataPortalContext context = request.Context;//MobileFormatter.Deserialize(request.PortalContextData) as DataPortalContext;
            DataPortalFacade portal = new DataPortalFacade();
            var result = new WcfResponse();
            try
            {
                result.ObjectData = portal.Fetch(bizType, criteria, context);
            }
            catch (Exception ex)
            {
                result.ErrorData = new ServiceExceptionDetail(ex);
            }
            return result;
        }

        /// <summary>
        /// Update a business object.
        /// </summary>
        /// <param name="request">The request parameter object.</param>
        [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        public WcfResponse Update(UpdateRequest request)
        {
            object obj = request.ObjectData;
            DataPortalContext context = request.Context;//MobileFormatter.Deserialize(request.PortalContextData) as DataPortalContext;
            DataPortalFacade portal = new DataPortalFacade();
            var result = new WcfResponse();
            try
            {
                result.ObjectData = portal.Update(obj, context);
            }
            catch (Exception ex)
            {
                result.ErrorData = new ServiceExceptionDetail(ex);
            }
            return result;
        }

        /// <summary>
        /// Delete a business object.
        /// </summary>
        /// <param name="request">The request parameter object.</param>
        [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        public WcfResponse Delete(CriteriaRequest request)
        {
            object criteria = request.CriteriaData;//GetCriteria(request.CriteriaData);
            Type bizType = Type.GetType(request.TypeName);
            DataPortalContext context = request.Context;//MobileFormatter.Deserialize(request.PortalContextData) as DataPortalContext;
            DataPortalFacade portal = new DataPortalFacade();
            var result = new WcfResponse();
            try
            {
                result.ObjectData = portal.Delete(bizType, criteria, context);
            }
            catch (Exception ex)
            {
                result.ErrorData = new ServiceExceptionDetail(ex);
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
        public WcfResponse Execute(ExcuteRequest request)
        {
            var actionInfo = request.ActionInfo;
            DataPortalContext context = request.Context;//MobileFormatter.Deserialize(request.PortalContextData) as DataPortalContext;
            DataPortalFacade portal = new DataPortalFacade();
            var result = new WcfResponse();
            try
            {
                //result.ObjectData = portal.Execute(actionInfo, context);
            }
            catch (Exception ex)
            {
                result.ErrorData = new ServiceExceptionDetail(ex);
            }
            return result;
        }

        #endregion

        #region Criteria
        /// <summary>
        /// 反序列化Criteria对象
        /// </summary>
        /// <param name="criteriaData"></param>
        /// <returns></returns>
        private static object GetCriteria(byte[] criteriaData)
        {
            object criteria = null;
            //if (criteriaData != null)
            //    criteria = Serialization.Serializers.BinarySerializer.Deserialize<Criteria>(criteriaData);
            return criteria;
        }

        #endregion
    }
}
